<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\service\adminService;
use think\Db;
class Astrict extends Common{
    //权限分类
    public function astrictList(){
        //查询权限分类数据
        $list = (new adminService())->astrictList();
        $this->assign('list',$list);
        //查询权限分类条数
        $number = count($list);
        $this->assign('number',$number);
        return $this->fetch('admin_astrict');
    }
    public function astrictAdd(){
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //验证表单信息
            $validate = $this->validate($data,[
                'title|权限名称' => 'require',
                'pid|上级权限' => 'require',
                'name|权限规则' => 'require',
            ]);
            if($validate !== true){
                $this->error($validate);
            }
            //判断分类名称是否重复
            $info = (new adminService())->astrictName($data['title']);
            if($info){
                $this->error('权限规则已存在');
            }
            // $data['time'] = date('Y-m-d H:i:s',time());
            //将数据插入数据库
            $result = (new adminService())->astrictAdd($data);
            //判断数据是否插入成功
            if($result){
                $this->success('添加权限规则成功',url('astrictList'));
            }else{
                $this->error('添加权限规则失败',url('astrictList'));
            }
        }else{
            return $this->fetch('admin_astrict');
        }
    }
    public function astrictEdit(){
        //查询权限分类数据
        $list = (new adminService())->astrictList();
        $this->assign('list',$list);
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('规则规则不存在');
            }
            $info = (new adminService())->astrictIdList($id);
            $this->assign('info',$info);
            return $this->fetch('astrict_edit');
        }else{
            $data = request()->post();
            $id = $data['id'];
            //验证表单信息
            $validate=  $this->validate($data,[
                'title|权限名称' => 'require',
                'pid|上级权限' => 'require',
                'name|权限规则' => 'require',
            ]);
            if($validate !== true){
                $this->error($validate);
            }
            //将数据插入数据库
            $result = (new adminService())->astrictUpdate($data);
            //判断数据是否插入成功
            if($result){
                $this->success('修改权限规则成功',url('astrictList'));
            }else{
                $this->error('修改权限规则失败',url('astrictEdit',['id' => $id]));
            }
        }
    }
    public function astrictDel(){
		$request = request();
		$id = $request->param('id');
		$validate = $this->validate($request->param(),[
			'id'	=>	'require|number'
		]);
		if($validate !== true){
			$this->error($validate);
        }
        //判断是否存在该数据
        $info = (new adminService())->astrictIdList($id);
        if(empty($info)){
            $this->error('该权限不存在');
        }
        //判断该权限下是否有子级权限
        $infos = (new adminService())->astrictZidList($id);
        if(!empty($infos)){
            $this->error('该权限下面有子级权限，不能删除');
        }
        $result = Db::name('auth_rule')->delete($id);
        if($result){
            $this->success('删除权限成功',url('astrictList'));
		}else{
            $this->success('删除权限失败',url('astrictList'));
		}
	}
}